#tool_beiwang工具 备忘录 用于记录个人的一些备忘可以设置提醒以便跟进
from django.shortcuts import render, redirect,get_object_or_404
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView,FormView
from django.contrib.auth.mixins import LoginRequiredMixin
####### 导入模型 #####
from .models import tool_beiwang
from .forms import tool_BeiWang_Form

class BeiWang(LoginRequiredMixin,ListView):
    model = tool_beiwang
    template_name = 'tool_beiwang_list.html'
    context_object_name = 'beiwangs'
    paginate_by = 10
class BeiWangDetail(LoginRequiredMixin,DetailView):
    model = tool_beiwang
    template_name = 'tool_beiwang_detail.html'
    context_object_name = 'beiwangs'
class BeiWangCreate(LoginRequiredMixin,CreateView):
    model = tool_beiwang
    success_url = '/beiwang/'
    template_name = 'tool_beiwang_add.html'
    form_class = tool_BeiWang_Form
    def form_valid(self, form):
        self.object = form.save(commit=False)
        adduser = self.request.user
        print(adduser)
        self.object.add_user = adduser
        self.object.save()
        return super().form_valid(form)
class BeiWangDelete(LoginRequiredMixin,DeleteView):
    model = tool_beiwang
    success_url = '/beiwang/'
    template_name = 'tool_beiwang_add.html'

    def get(self, request, pk, *args, **kwargs):
        beiwang = get_object_or_404(tool_beiwang, pk=pk)
        if beiwang.add_user == request.user:
            return super().delete(request, pk, *args, **kwargs)
        else:
            return redirect('/')




